package top.maof.book.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.maof.book.params.BooksParam;
import top.maof.book.annotation.Cache;
import top.maof.book.annotation.Hot;
import top.maof.book.aspect.HotAspect;
import top.maof.book.model.Book;
import top.maof.book.model.Role;
import top.maof.book.result.ApiResponse;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.result.Result;
import top.maof.book.service.BookService;
import top.maof.book.type.HotType;
import top.maof.book.utils.QueryUtil;
import top.maof.book.vo.PageVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by CodeX4J.
 */
@Controller
@RequestMapping("/book")
@ResponseBody
public class BookController {


    @Autowired
    BookService bookService;

    @ApiOperation("新建")
    @PostMapping("/create")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse create(@RequestBody Book book) {
        return ApiResponse.success(bookService.save(book));
    }

    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse deleteById(@PathVariable Integer id) {
        bookService.delete(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse deleteByIdInBatch(@RequestParam String ids) {
        String[] idArray = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Integer.valueOf(id));
        }
        bookService.removeBatchByIds(idList);
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    @RequiresRoles({Role.ADMIN})
    public ApiResponse updateById(@RequestBody Book book) {
        bookService.updateById(book);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public ApiResponse<PageData<Book>> findPage(@RequestBody PageWrap<Book> pageWrap) {
        PageData<Book> page = bookService.findPage(pageWrap);
        return ApiResponse.success(page);
    }


    @PostMapping(value = "")
    @RequiresRoles(value = {Role.ADMIN})
    public Result add(Book book) throws Exception{
        if (bookService.add(book) > 0)
            return Result.success();
        return Result.response_400();
    }

    @GetMapping("/{id}")
    @Hot(value = "#id", type = HotType.CLICK)
    public Result find(@PathVariable("id") int id) {
        Book book = bookService.find(id);
        if (book != null) {
            return Result.success(book);
        }
        return Result.response_404();
    }

    @PutMapping("")
    @RequiresRoles(value = {Role.ADMIN})
    public Result update(Book book) {
        if (bookService.update(book) > 0)
            return Result.success();
        return Result.response_400();
    }

    @DeleteMapping("/{id}")
    @RequiresRoles(value = {Role.ADMIN})
    public Result delete(@PathVariable("id") int id) {
        if (bookService.delete(id) > 0)
            return Result.success();
        return Result.response_400();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Result all(@RequestParam Map map) {
        PageVo pageBo = new PageVo();
        pageBo.setList(bookService.select(QueryUtil.parse(map)));
        pageBo.setCount(bookService.selectCount(map));
        return Result.success(pageBo);
    }


    @PostMapping("/list")
    public Result<PageVo<Book>> list(@RequestBody BooksParam booksParam) {
        PageVo<Book> list = bookService.list(booksParam);
        return Result.success(list);
    }


    @PostMapping("/gets")
    public Result gets(Integer[] ids) {
        List<Integer> list1 = new ArrayList<>();
        if (ids != null && ids.length > 0)
            for (Integer s : ids) {
                list1.add(s);
            }
        return Result.success(bookService.selectList(list1));
    }

    @Cache(name = "#id", ex = "book")
    @GetMapping("/get/{id}")
    @Deprecated
    @Hot(value = "#id", type = HotType.CLICK)
    public Result get(@PathVariable("id") int id) {
        Book book = bookService.find(id);
        if (book != null) {
            return Result.success(book);
        }
        return Result.response_404();
    }

    @GetMapping("/author/{author}")
    public Result author(@PathVariable("author") String author) {
        List<Book> books = bookService.selectByAuthor(author);
        return Result.success(books);
    }

    @GetMapping("/classifies/{sourceSite}")
    public Result<List<String>> classifies(@PathVariable("sourceSite") String sourceSite) {
        List<String> classifies = bookService.classifies(sourceSite);
        return Result.success(classifies);
    }
}
